package com.cskaoyan.service.back;

import com.cskaoyan.bean.po.CommonData;
import com.cskaoyan.bean.po.MarketCategory;
import com.cskaoyan.bean.po.MarketCategoryExample;
import com.cskaoyan.bean.vo.back.CreateGoodsCategoryVO;
import com.cskaoyan.bean.vo.back.GetGoodsLabelVO;
import com.cskaoyan.bean.vo.back.ShowGoodsCategoryVO;
import com.cskaoyan.mapper.MarketCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 商品类目
 * @auther 10-何兴绕
 * @Date 2022年10月11日 18:37
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    /**
     * 展示商品类
     * @return
     */
    @Override
    public CommonData getGoodsCategory() {
        MarketCategoryExample exampleCount = new MarketCategoryExample();
        MarketCategoryExample.Criteria countCriteria = exampleCount.createCriteria();
        countCriteria.andDeletedEqualTo(false);
        long count = marketCategoryMapper.countByExample(exampleCount);
        MarketCategoryExample example = new MarketCategoryExample();
        example.setOrderByClause("update_time desc");
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(example);

        List<ShowGoodsCategoryVO> goodsCategoryVOS = new ArrayList<>();

        for (MarketCategory m : marketCategories) {
            if (m.getPid() == 0) {

                ShowGoodsCategoryVO s = new ShowGoodsCategoryVO();
                s.setId(m.getId());
                s.setName(m.getName());
                s.setKeywords(m.getKeywords());
                s.setDesc(m.getDesc());
                s.setIconUrl(m.getIconUrl());
                s.setPicUrl(m.getPicUrl());
                s.setLevel(m.getLevel());
                goodsCategoryVOS.add(s);
            }
        }

        for (ShowGoodsCategoryVO g : goodsCategoryVOS) {
            List<ShowGoodsCategoryVO.Children> childrens = new ArrayList<>();
            for (MarketCategory m : marketCategories) {
                if (m.getPid().equals(g.getId())) {
                    ShowGoodsCategoryVO.Children c = new ShowGoodsCategoryVO.Children();
                    c.setId(m.getId());
                    c.setName(m.getName());
                    c.setKeywords(m.getKeywords());
                    c.setDesc(m.getDesc());
                    c.setIconUrl(m.getIconUrl());
                    c.setPicUrl(m.getPicUrl());
                    c.setLevel(m.getLevel());
                    childrens.add(c);
                }
            }
            g.setChildren(childrens);
        }
        CommonData<ShowGoodsCategoryVO> commonData = new CommonData<>();

        commonData.setTotal((int) count);
        commonData.setPages(1);
        commonData.setLimit(goodsCategoryVOS.size());
        commonData.setPage(1);
        commonData.setList(goodsCategoryVOS);
        return commonData;
    }

    /**
     * 获取商品类目标签
     * @return
     */
    @Override
    public CommonData getGoodsLabel() {
        MarketCategoryExample exampleCount = new MarketCategoryExample();
        MarketCategoryExample.Criteria countCriteria = exampleCount.createCriteria();
        countCriteria.andDeletedEqualTo(false);
        long count = marketCategoryMapper.countByExample(exampleCount);
        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(example);
        List<GetGoodsLabelVO> getGoodsLabelVOS = new ArrayList<>();
        for (MarketCategory m : marketCategories) {
            if (m.getPid() == 0){
                GetGoodsLabelVO getGoodsLabelVO = new GetGoodsLabelVO(m.getId(), m.getName());
                getGoodsLabelVOS.add(getGoodsLabelVO);
            }
        }
        CommonData<GetGoodsLabelVO> commonData = new CommonData<>();
        commonData.setTotal((int) count);
        commonData.setPages(1);
        commonData.setLimit(getGoodsLabelVOS.size());
        commonData.setPage(1);
        commonData.setList(getGoodsLabelVOS);
        return commonData;
    }

    /**
     * 创建商品类目
     * @param m
     * @return
     */
    @Override
    public CreateGoodsCategoryVO createGoodsCategory(MarketCategory m) {
        m.setAddTime(new Date());
        m.setUpdateTime(new Date());
        marketCategoryMapper.insertSelective(m);
        MarketCategory ma = marketCategoryMapper.selectByPrimaryKey(m.getId());
        CreateGoodsCategoryVO categoryVO = new CreateGoodsCategoryVO();
        categoryVO.setId(ma.getId());
        categoryVO.setName(ma.getName());
        categoryVO.setKeywords(ma.getKeywords());
        categoryVO.setDesc(ma.getDesc());
        categoryVO.setPid(ma.getPid());
        categoryVO.setIconUrl(ma.getIconUrl());
        categoryVO.setPicUrl(ma.getPicUrl());
        categoryVO.setLevel(ma.getLevel());
        categoryVO.setAddTime(ma.getAddTime());
        categoryVO.setUpdateTime(ma.getUpdateTime());
        return categoryVO;
    }

    /**
     * 更新商品类目
     * @param s
     * @return
     */
    @Override
    public int updateGoodsCategory(ShowGoodsCategoryVO s) {
        MarketCategory m = new MarketCategory();
        m.setId(s.getId());
        m.setName(s.getName());
        m.setKeywords(s.getKeywords());
        m.setDesc(s.getDesc());
        m.setIconUrl(s.getIconUrl());
        m.setPicUrl(s.getPicUrl());
        m.setLevel(s.getLevel());
        m.setUpdateTime(new Date());
        if (s.getPid() != null){
            m.setPid(s.getPid());
        }
        marketCategoryMapper.updateByPrimaryKeySelective(m);

        List<ShowGoodsCategoryVO.Children> children = s.getChildren();
        if (children != null){
            for (ShowGoodsCategoryVO.Children child : children) {
                MarketCategory childC = new MarketCategory();
                childC.setId(child.getId());
                childC.setName(child.getName());
                childC.setKeywords(child.getKeywords());
                childC.setDesc(child.getDesc());
                childC.setIconUrl(child.getIconUrl());
                childC.setPicUrl(child.getPicUrl());
                childC.setLevel(child.getLevel());
                childC.setPid(child.getPid());
                childC.setUpdateTime(new Date());
                marketCategoryMapper.updateByPrimaryKeySelective(childC);
            }
        }
        return 200;
    }

    /**
     * 删除商品类目
     * @param s
     * @return
     */
    @Override
    public int deleteGoodsCategory(ShowGoodsCategoryVO s) {
        List<ShowGoodsCategoryVO.Children> children = s.getChildren();

        for (ShowGoodsCategoryVO.Children child : children) {
            MarketCategoryExample childExample = new MarketCategoryExample();
            MarketCategoryExample.Criteria exampleCriteria = childExample.createCriteria();
            exampleCriteria.andIdEqualTo(child.getId());
            MarketCategory childCategory = new MarketCategory();
            childCategory.setName(child.getName());
            childCategory.setKeywords(child.getKeywords());
            childCategory.setDesc(child.getDesc());
            childCategory.setIconUrl(child.getIconUrl());
            childCategory.setPicUrl(child.getPicUrl());
            childCategory.setLevel(child.getLevel());
            childCategory.setDeleted(true);
            marketCategoryMapper.updateByExampleSelective(childCategory, childExample);
        }
        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        MarketCategory marketCategory = new MarketCategory();
        criteria.andIdEqualTo(s.getId());
        marketCategory.setName(s.getName());
        marketCategory.setKeywords(s.getKeywords());
        marketCategory.setDesc(s.getDesc());
        marketCategory.setIconUrl(s.getIconUrl());
        marketCategory.setPicUrl(s.getPicUrl());
        marketCategory.setLevel(s.getLevel());
        marketCategory.setDeleted(true);
        int code = marketCategoryMapper.updateByExampleSelective(marketCategory, example);
        return code;
    }
}
